<template>
  <div class="register-page">
    <div class="register">
      <div class="login__title">{{ $t('title') }}</div>
      <div class="login__subtitle">{{ $t('hrRegisterTitle') }}</div>
      <el-form :model="registerForm" :rules="rules" ref="refRegisterForm" @keyup.enter.native="onRegister" size="medium" :label-width="labelWidth">
        <el-row>
          <el-col :span="8">
            <el-form-item label="企业名称" prop="enterpriseName">
              <el-input v-model="registerForm.enterpriseName" placeholder="请选择企业名称" readonly="readonly">
                <i slot="suffix" class="el-input__icon el-icon-search" @click="findEnterprise"></i>
              </el-input>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="用户名" prop="username">
              <el-input v-model="registerForm.username" placeholder="请输入用户名" maxlength="64"></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="姓名" prop="name">
              <el-input v-model="registerForm.name" placeholder="请输入姓名" maxlength="64"></el-input>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="8">
            <el-form-item label="密码" prop="password">
              <el-input v-model="registerForm.password" placeholder="请输入密码" maxlength="255"></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="联系电话" prop="contactPhone">
              <el-input v-model="registerForm.contactPhone" placeholder="请输入联系电话" maxlength="64"></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="邮箱" prop="email">
              <el-input v-model="registerForm.email" placeholder="请输入邮箱" maxlength="64"></el-input>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="8">
            <el-form-item label="确认密码" prop="password">
              <el-input v-model="registerForm.password2" placeholder="请确认密码" maxlength="255"></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item prop="verificationCode" label="验证码">
              <el-input v-model="registerForm.verificationCode" placeholder="请输入验证码"></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item>
              <canvas id="captchaCanvas" width="150" height="50" @click="drawCaptcha"></canvas>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12" :offset="6">
            <el-button type="primary" @click="onRegister" :loading="loading">注册</el-button>
            <el-button @click="onReset">重置</el-button>
            <el-button type="primary" @click="onBack" :loading="loading">返回登录</el-button>
          </el-col>
        </el-row>
      </el-form>
    </div>
    <div class="footer">
      <span>版权所有 &copy; 2024 - 2026 <a href="http://www.52ejn.com" target="_blank">源码学习网</a></span><a href="http://beian.miit.gov.cn" target="_blank">粤ICP备2024308896号-1</a>
    </div>
    <el-dialog :visible.sync="enterpriseSelectorVisible" v-if="enterpriseSelectorVisible" title="企业信息选择器(双击行选中)" :top="dialogTop" :width="dialogWidth" :append-to-body="true">
      <enterprise-selector @enterprise-selected-event="handleEnterpriseSelectedEvent">
      </enterprise-selector>
    </el-dialog>
  </div>
</template>

<script>
import captchaMixin from "@/mixins/captchaMixin";
import EnterpriseSelector from "@/views/enterprise/EnterpriseSelector.vue";
import recruiterApi from "@/api/recruiterApi";

export default {
  name: 'HrRegister',
  components: {EnterpriseSelector},
  mixins: [
    captchaMixin
  ],
  data () {
    return {
      registerForm: {
        enterpriseId: '',
        enterpriseName: '',
        username: '',
        name: '',
        password: '',
        password2: '',
        contactPhone: '',
        email: '',
        verificationCode: ''
      },
      labelWidth: '80px',
      rules: {
        enterpriseName: [
          { required: true, message: '请输入企业名称', trigger: 'blur' }
        ],
        username: [
          { required: true, message: '请输入用户名', trigger: 'blur' }
        ],
        name: [
          { required: true, message: '请输入姓名', trigger: 'blur' }
        ],
        password: [
          { required: true, message: '请输入密码', trigger: 'blur' }
        ],
        password2: [
          { required: true, message: '请确认密码', trigger: 'blur' }
        ],
        contactPhone: [
          { required: true, message: '请输入联系电话', trigger: 'blur' }
        ],
        email: [
          { required: true, message: '请输入邮箱', trigger: 'blur' }
        ],
        verificationCode: [
          { required: true, message: '请输入验证码', trigger: 'blur' }
        ]
      },
      loading: false,
      enterpriseSelectorVisible: false,
    }
  },
  created () {
  },
  methods: {
    onReset () {
      this.registerForm = {
        enterpriseId: '',
        enterpriseName: '',
        username: '',
        name: '',
        password: '',
        password2: '',
        contactPhone: '',
        email: '',
        verificationCode: ''
      }
      this.provinceCityArea = []
    },
    onRegister () {
      this.$refs.refRegisterForm.validate((valid) => {
        if (valid) {
          if (this.registerForm.password !== this.registerForm.password2) {
            this.$message.error('两次输入的密码不一致，请重新输入')
            this.registerForm.password = ''
            this.registerForm.password2 = ''
            this.refreshCaptcha()
            return
          }
          if (this.generatedVerificationCode.toLowerCase() !== this.registerForm.verificationCode.toLowerCase()) {
            this.$message.error('验证码错误')
            this.refreshCaptcha()
            return
          }
          this.loading = true
          recruiterApi.save(this.recruiterForm)
              .then((resp) => {
                if (resp.code === 1) {
                  this.$message.success('注册成功')
                  this.$router.push({ path: '/Login', query: {type: 2} })
                } else {
                  this.$message.error('注册失败:' + (resp.msg ? resp.msg : ''))
                  this.refreshCaptcha()
                  this.loading = false
                }
              })
              .catch(error => {
                console.log('注册失败', error)
                this.$message.error('注册失败')
                this.refreshCaptcha()
                this.loading = false
              })
        } else {
          this.refreshCaptcha()
          return false
        }
      })
    },
    refreshCaptcha () {
      this.registerForm.verificationCode = ''
      this.drawCaptcha()
    },
    findEnterprise () {
      this.enterpriseSelectorVisible = true
    },
    handleEnterpriseSelectedEvent (selectedEnterprise) {
      this.enterpriseSelectorVisible = false
      if (selectedEnterprise && 'enterpriseId' in selectedEnterprise && this.registerForm.enterpriseId !== selectedEnterprise['enterpriseId']) {
        this.registerForm.enterpriseId = selectedEnterprise['enterpriseId']
        this.registerForm.enterpriseName = selectedEnterprise['enterpriseName']
      }
    },
    onBack () {
      this.$router.push({ name: 'Login', query: {type: 2}})
    }
  }
}
</script>

<style lang="scss">
@import "@/assets/styles/register";
</style>
